package com.ruoyi.heima.listener;

import com.ruoyi.common.core.constant.MqConstants;
import com.ruoyi.heima.domain.po.News;
import com.ruoyi.heima.enums.NewsStatus;
import com.ruoyi.heima.service.INewsService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OrderDelayMessageListener {

    private final INewsService newsService;

    private final RabbitTemplate rabbitTemplate;
    /**
     * 监听延迟消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstants.DELAY_NEWS_QUEUE, durable = "true"),
            exchange = @Exchange(value = MqConstants.DELAY_NEWS_EXCHANGE, type = ExchangeTypes.TOPIC, durable = "true",
                   delayed = "true"),
            key = MqConstants.DELAY_PUBLISH_ROUTING_KEY
    ))
    public void listenOrderDelayMsg(News news){
//        newsService.pushNews(newsIds);
        // 根据新闻id查询新闻
//        News news = newsService.getById(newsId);
        // 更改新闻状态
        news.setStatus(NewsStatus.PUBLISH);
        newsService.updateById(news);
        // 发送消息,同步ES
        rabbitTemplate.convertAndSend(MqConstants.NEWS_EXCHANGE, MqConstants.NEWS_ROUTING_KEY, news);
//        newsId.forEach(System.out::println);
        /*newsService.updateNewsStatusByNewsId(newsIds);*/
    }
}